package cn.com.controler.Util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {

    static DataSource db = null;
    //加载获取资源
    static {
        Properties p = new Properties();
        try {
            p.load(JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            db = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建连接对象
    public static Connection getConnection(){
        try {
            //加载注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接对象
            return DriverManager.getConnection("jdbc:mysql://localhost:3306/bmdlt","root","root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //关闭资源
    public static void close(Connection coll, PreparedStatement statement, ResultSet resultSet){
        try {
            if(coll!=null)
            {
                coll.close();
            }
            if(statement!=null)
            {
                statement.close();
            }
            if(resultSet!=null)
            {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
